package com.ftp.web.spider.utils.downloadutils;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

@Slf4j
@Component
public class WebDownLoadTools {

    private Logger logger= LoggerFactory.getLogger(WebDownLoadTools.class);

    public void downloadHttpLink(String savePath,String link) {
        if (link.isBlank()) {
            return;
        }
        try {
            URL url = new URL(link);
            HttpURLConnection conn;
            conn = (HttpURLConnection)url.openConnection();
            //设置超时时间
            conn.setConnectTimeout(3*1000);
            //设置请求头
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.50");
            //conn.setRequestProperty("lfwywxqyh_token",toekn);

            //打开输入流
            InputStream inputStream = conn.getInputStream();
            //设置读取字节码长度
            byte[] getData = readInputStream(inputStream);

            //设置文件存储路径
            File saveDir = new File(savePath);
            if(!saveDir.exists()){
                saveDir.mkdir();
            }
            File file = new File(saveDir+File.separator+link.substring(link.lastIndexOf("/")+1));
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(getData);
            if(fos!=null){
                fos.close();
            }
            if(inputStream!=null){
                inputStream.close();
            }
            logger.info("链接{} 存储于 路径{}",link,savePath);
        } catch (IOException e) {
            logger.error("异常 链接 {} 保存路径{} 异常信息{}",link,savePath,e.getMessage());
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        logger.info("链接{} 存储于 路径{}",link,savePath);
       // System.out.println("info:"+link+" download success");
    }

    public  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }
}
